package com.sdap2.service.impl;

import com.sdap2.common.exception.BusinessException;
import com.sdap2.common.utils.WebClientUtil;
import com.sdap2.dao.CameraDataDAO;
import com.sdap2.dao.DeviceDAO;
import com.sdap2.entity.CameraData;
import com.sdap2.entity.Device;
import com.sdap2.entity.enums.DeviceType;
import com.sdap2.service.CameraService;
import com.sdap2.service.DeviceService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class CameraServiceImpl implements CameraService {
    
    private final DeviceDAO deviceDAO;
    private final CameraDataDAO cameraDataDAO;
    private final DeviceService deviceService;
    private final WebClientUtil webClientUtil;
    
    @Override
    @Transactional
    public String capture(String deviceSerial, String token) {
        // 去除序列号前后的空格
        String trimmedSerial = deviceSerial.trim();
        
        Device device = deviceDAO.findBySerialNumber(trimmedSerial)
                .orElseThrow(() -> new BusinessException("设备不存在"));
                
        if (!deviceService.checkDeviceOwnership(device.getId(), token)) {
            throw new BusinessException("无权操作此设备");
        }
        
        if (!DeviceType.CAMERA.equals(device.getDeviceType())) {
            throw new BusinessException("该设备不是摄像头");
        }
        
        System.out.println("正在抓拍设备: " + trimmedSerial);
        
        String picUrl = webClientUtil.captureCamera(trimmedSerial);
        
        // 保存抓拍记录
        CameraData cameraData = new CameraData();
        cameraData.setDevice(device);
        cameraData.setPicUrl(picUrl);
        cameraDataDAO.save(cameraData);
        
        return picUrl;
    }
    
    @Override
    @Transactional(readOnly = true)
    public Page<CameraData> getHistory(String deviceSerial, Pageable pageable, String token) {
        Device device = deviceDAO.findBySerialNumberWithGreenhouse(deviceSerial)
                .orElseThrow(() -> new BusinessException("设备不存在"));
                
        if (!deviceService.checkDeviceOwnership(device.getId(), token)) {
            throw new BusinessException("无权操作此设备");
        }
        
        return cameraDataDAO.findAllByDeviceId(device.getId(), pageable);
    }
    
    @Override
    @Transactional(readOnly = true)
    public Page<CameraData> getHistory(String deviceSerial, LocalDateTime startTime, 
                                     LocalDateTime endTime, Pageable pageable, String token) {
        Device device = deviceDAO.findBySerialNumberWithGreenhouse(deviceSerial)
                .orElseThrow(() -> new BusinessException("设备不存在"));
                
        if (!deviceService.checkDeviceOwnership(device.getId(), token)) {
            throw new BusinessException("无权操作此设备");
        }
        
        return cameraDataDAO.findAllByDeviceIdAndCreateTimeBetween(
                device.getId(), startTime, endTime, pageable);
    }
    
    @Override
    public CameraData getLatestCapture(String deviceSerial, String token) {
        Device device = deviceDAO.findBySerialNumber(deviceSerial)
                .orElseThrow(() -> new BusinessException("设备不存在"));

        if (!deviceService.checkDeviceOwnership(device.getId(), token)) {
            throw new BusinessException("无权操作此设备");
        }

        return cameraDataDAO.findFirstByDeviceIdOrderByCreateTimeDesc(device.getId())
                .orElseThrow(() -> new BusinessException("未找到抓拍记录"));
    }
} 